package cn.edu.ncu.housinghub.entity.contract;

import cn.edu.ncu.housinghub.dto.request.contract.ContractRequest;
import cn.edu.ncu.housinghub.entity.house.House;
import cn.edu.ncu.housinghub.entity.user.Agent;
import cn.edu.ncu.housinghub.entity.user.Landlord;
import cn.edu.ncu.housinghub.entity.user.User;
import jakarta.validation.constraints.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 合同主信息
 */
@Data
public class Contract {
    private Long contractId; // 主键

    @NotNull(message = "租客信息不能为空")
    private User tenant; // 租客信息

    @NotNull(message = "房源信息不能为空")
    private House house; // 房源信息

    @NotNull(message = "房东信息不能为空")
    private Landlord landlord; // 房东ID

    public Contract(ContractRequest contractRequest) {
        this.tenant = new User(contractRequest.getTenantId());
        this.house = new House(contractRequest.getHouseId());
        this.landlord = new Landlord(contractRequest.getLandlordId());
        this.agent = new Agent(contractRequest.getAgent().getAgentId());
        this.startDate = contractRequest.getStartDate();
        this.endDate = contractRequest.getEndDate();
        this.monthRent = contractRequest.getMonthRent();
        this.deposit = contractRequest.getDeposit();
        this.paymentType = contractRequest.getPaymentType();
        this.applyTime=LocalDateTime.now();
        this.signStatus=0;

    }

    // 若房源已经有中介代理，则必须有中介信息
    @AssertTrue(message = "若房源已经有中介代理，则必须有中介信息")
    public boolean isAgentOrLandlord() {
        return agent != null || landlord != null;
    }

    private Agent agent; // 中介信息

    @NotNull(message = "租赁开始日期不能为空")
    @Future(message = "租赁开始日期必须是未来日期")
    private LocalDate startDate; // 租赁开始日期

    @NotNull(message = "租赁结束日期不能为空")
    @Future(message = "租赁结束日期必须是未来日期")
    private LocalDate endDate; // 租赁结束日期

    @NotNull(message = "月租金不能为空")
    @Positive(message = "月租金必须大于0")
    private BigDecimal monthRent; // 月租金

    @Positive(message = "押金必须大于0")
    private BigDecimal deposit; // 押金

    @NotNull(message = "支付方式不能为空")
    @Min(value = 1, message = "支付方式必须为有效值")
    @Max(value = 3, message = "支付方式必须为有效值")
    private Integer paymentType; // 支付方式（1=月付，2=季付，3=年付）

    private String refuseReason; // 拒绝原因

    @NotNull(message = "签署状态不能为空")
    @Min(value = 0, message = "签署状态必须为有效值")
    @Max(value = 4, message = "签署状态必须为有效值")
    private Integer signStatus; // 签署状态（0=用户已发起预约，1=房东已同意，2=房东已拒绝，3=租客已签署，4=房东已签署，5=合同已生效）
    private String contractUrl; // 合同PDF路径（OSS）

    private LocalDateTime applyTime; // 申请时间
    private LocalDateTime signDate; // 签署日期
    private ContractContentInfo contractContentInfo; // 合同内容信息


}
